<?php
namespace app\index\controller;

use think\Controller;

use think\Model; 

class Admin extends Controller
{
    public function index()
    {
        if(!$this->issetCookie())
            $this->error('未登录','Admin/login',5);
        else{
            $this->assign('admin',$_COOKIE["admin"]);
            return $this->fetch();
        }       
    }

    public function issetCookie()
    {
        if (isset($_COOKIE["admin"]))
            return true;
        else
            return false;
    }

    public function alluser()
    {
        if(!$this->issetCookie())
            $this->error('未登录','Admin/login',5);
        $this->assign('admin',$_COOKIE["admin"]);
        $message=Model('message');
        $res=$message->paginate(10);
        $this->assign('user',$res);
        return $this->fetch();
    }

    public function ajax()
    {
        $msg=input('get.msg');
        $regexp_number='/^[0-9]*$/';    
        $message=Model('message');
        if(preg_match($regexp_number,$msg)){
            $user=Model('user');
            $people=$user->where('username','like',$msg.'%')->select();
            if($people == "")
                return json_encode("false");
            else{
                $array=array();
                foreach ($people as $value) {
                    $res=$message->where('uid',$value['id'])->find();
                    array_push($array, $res);
                }
                return $array;
            }
        }
        else{
            $res=$message->where('xm','like','%'.$msg.'%')->select();
            if($res == "")
                return json_encode("false");           
            else
                return $res;
        }
    }

    public function alluser_select()
    {
        $xh=input('get.xh');
        $xm=input('get.xm');
        $message=Model('message');
        $this->assign('admin',$_COOKIE["admin"]); 
        if($xh=="" && $xm==""){                     
            $res=$message->select();
            $this->assign('user',$res);
            return $this->fetch('alluser'); 
        }
        else{
            if($xm!=""){
                $res=$message->where('xm',$xm)->select();
                $this->assign('user',$res);
                return $this->fetch('alluser');
            }
            else{
                $user=Model('user');
                $msg=$user->where('username',$xh)->find();
                $res=$message->where('uid',$msg['id'])->select();
                $this->assign('user',$res);
                return $this->fetch('alluser');
            }
            
        }
    }

    public function forbid()
    {
        $id=input('get.id');
        $user=Model('user');
        $message=Model('message');
        $res=$user->where('id',$id)->find();
        $forbid=!$res['forbid'];
        $res['forbid']=$forbid;
        $res->save();
        $msg=$message->where('id',$id)->find();
        $msg['forbid']=$forbid;
        $msg->save();
        $this->assign('admin',$_COOKIE["admin"]);
        $res=$message->select();
        $this->assign('user',$res);
        $this->success('修改成功','Admin/alluser',5);
    }

    public function delete()
    {
        $id=input('get.id');
        $user=Model('user');
        $message=Model('message');
        $user->where('id',$id)->delete();
        $message->where('uid',$id)->delete();
        $this->assign('admin',$_COOKIE["admin"]);
        $res=$message->select();
        $this->assign('user',$res);
        $this->success('删除成功','Admin/alluser',5);
    }

    public function comdlt()
    {
        $id=input('get.id');
        $common=Model('common');
        $res=$common->where('id',$id)->find();
        $res['dlt']=1;
        $res->save();
        $res=$common->where('dlt',0)->select();
        $this->assign('common',$res);
        $this->assign('admin',$_COOKIE["admin"]);
        $this->success('删除成功','Admin/common',5);
    }

    public function common()
    {
        if(!$this->issetCookie())
            $this->error('未登录','Admin/login',5);
        $common=Model('common');
        $res=$common->where('dlt',0)->select();
        $this->assign('common',$res);
        $this->assign('admin',$_COOKIE["admin"]);
        return $this->fetch();
    }

    public function login()
    {
        return $this->fetch();
    }

    public function match()
    {
        if(!$this->issetCookie())
            $this->error('未登录','Admin/login',5);
        else{
            $match=Model('match');
            $res=$match->where('id',1)->find();
            $this->assign('text',$res['text']);
            $this->assign('admin',$_COOKIE["admin"]);
            return $this->fetch();
        }          
    }

    public function match_update()
    {
        $text=input('get.text');
        $match=Model('match');
        $res=$match->where('id',1)->find();
        $res['text']=$text;
        $res->save();
        $this->success('更新成功','Admin/match',5);
    }

    public function active()
    {
        if(!$this->issetCookie())
            $this->error('未登录','Admin/login',5);
        else{
            $active=Model('active');
            $res=$active->select();
            $this->assign('res',$res);
            $this->assign('admin',$_COOKIE["admin"]);
            return $this->fetch();
        }          
    }

    public function active_update()
    {
        $id=input('param.id');
        $text=input('get.text');
        $time=input('get.time');
        $active=Model('active');
        $res=$active->where('id',$id)->find();
        $res['text']=$text;
        $res['time']=$time;
        $res->save();
        $this->success('更新成功','Admin/active',5);
    }

    public function user()
    {
        if(!$this->issetCookie())
            $this->error('未登录','Admin/login',5);
        else{
            $usr="";
            $this->assign('usr',$usr);
            $this->assign('admin',$_COOKIE["admin"]);
            return $this->fetch();
        }          
    }

    public function user_select()
    {
        $username=input('get.username');
        $name=input('get.name');
        $message=Model('message');
        $user=Model('user');
        if($username==""){            
            $res=$message->where('xm',$name)->find();
            if($res!=""){
                $msg=$user->where('id',$res['uid'])->find();
                $res['lv']=$msg['lv'];
            }
            $this->assign('usr',$res);
        }
        else{           
            $res=$user->where('username',$username)->find();
            $msg=$message->where('uid',$res['id'])->find();
            if($res!="")
                $msg['lv']=$res['lv'];
            $this->assign('usr',$msg);
        }
        $this->assign('admin',$_COOKIE['admin']);
        return $this->fetch('user');
    }

    public function user_update()
    {
        $xm=input('post.s_name');
        $type=input('post.s_type');
        $wx=input('post.s_wx');
        $qq=input('post.s_qq');
        $dh=input('post.s_dh');
        $email=input('post.s_email');
        $psw=input('post.s_psw');
        $message=Model('message');
        $user=Model('user');
        $res=$message->where('xm',$xm)->find();
        $msg=$user->where('id',$res['uid'])->find();
        if($psw==""){           
            $res['wx']=$wx;
            $res['qq']=$qq;
            $res['dh']=$dh;
            $res['email']=$email;
            if($type=="不可教球")
                $msg['lv']=0;
            else
                $msg['lv']=1;
        }
        else{
            $res['wx']=$wx;
            $res['qq']=$qq;
            $res['dh']=$dh;
            $res['email']=$email;           
            $msg['password']=md5($psw);
            if($type=="不可教球")
                $msg['lv']=0;
            else
                $msg['lv']=1;
        }
        $res->save();
        $msg->save();
        $this->success('更新成功','Admin/user',5);
    }

    public function cookie()
    {
        $name=input('post.name');
        $psw=input('post.psw');
        $model = Model('admin');
        $res = $model->where('username',$name)->where('password',md5($psw))->find();
        if($res == ""){
            $this->error('用户名或密码错误','login',5);

        }
        else{
            cookie('admin',$name);
            $this->assign('admin',$name);

            return $this->fetch('index');           
        }
    }
}